package com.sitemap.domain;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Hugo
 */
public class Url {

    /**
     * 起始信息
     */
    private String headInfo;

    /**
     * 结束信息
     */
    private String tailInfo;

    /**
     * 网页的地址
     */
    private String ioc;

    /**
     * 网页相对于其他页面的优先权,该值为0.0 - 1.0之间的某个值，该值越大优先级就越高
     */
    private double priority;

    /**
     * 指定链接的最后更新时间，这里必须用ISO 8601中指定的时间格式进行描述
     * 本应用使用的格式为：年月日小时分钟秒：YYYY-MM-DDThh:mmTZD(2005-06-04T10:37:30+08:00)
     */
    private String lastmod;

    /**
     * 链接可能会出现的更新频率,概属性的属性值为ChangeFreq类中的某一枚举值
     */
    private String changeFreq;

    public String getHeadInfo() {
        return "<url>";
    }

    public String getTailInfo() {
        return "</url>";

    }

    public void setIoc(String ioc) {
        this.ioc = ioc;
    }

    public String getIoc() {
        return "<loc>" + this.ioc + "</loc>";
    }

    public String getPriority() {
        return "<priority>" + this.priority + "</priority>";
    }

    public void setPriority(double priority) {
        // 处理设置值为0-1之间
        priority = (priority < 0) ? 0 : priority;
        priority = Math.min(priority, 1.0);
        this.priority = priority;
    }

    public String getLastmod() {
        String dateType = "YYYY-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        Date rightNow = new Date(System.currentTimeMillis());
        String dateStr = sdf.format(rightNow);
        return "<lastmod>" + dateStr + "</lastmod>";
    }

    public void setChangeFreq(String changeFreq) {
        if (!ChangeFreq.isContent(changeFreq)) {
            changeFreq = ChangeFreq.ALWAYS.getChangeName();
        }
        this.changeFreq = changeFreq;
    }

    public String getChangeFreq() {
        return "<changefreq>" + changeFreq + "</changefreq>";
    }

    public String createUrl() {
        return getHeadInfo() + "\n" + getIoc() + "\n" + getPriority() + "\n" + getLastmod() + "\n" + getChangeFreq()
                + "\n" + getTailInfo();
    }

}
